package com.example.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.server.entity.Food;
import com.example.server.entity.Order;
import com.example.server.service.FoodService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Food)表控制层
 *
 * @author makejava
 * @since 2022-11-21 09:10:57
 */
@RestController
@RequestMapping("food")
public class FoodController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private FoodService foodService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param food 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Food> page, Food food) {
        return success(this.foodService.page(page, new QueryWrapper<>(food)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.foodService.getById(id));
    }

    //根据食物类型查找出全部的食物
    @GetMapping("/selectbytyle/{id}")
    public R selectselectbytyle(@PathVariable Serializable id) {
        R r =new R<>();
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("foodtype",id);
        List<Food>  f=foodService.getBaseMapper().selectList(queryWrapper);
        r.setData(f);
        r.setMsg("执行成功");
        r.setCode(200);
        return r;
    }

    /**
     * 新增数据
     *
     * @param food 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Food food) {
        return success(this.foodService.save(food));
    }

    /**
     * 修改数据
     *
     * @param food 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Food food) {
        return success(this.foodService.updateById(food));
    }

    /**
     * 删除数据
     * @return 删除结果
     */
    @PutMapping("/delete/{id}")
    public R delete(@PathVariable Serializable id) {
        return success(this.foodService.removeById(id));
    }
}

